﻿using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace aztall.Module.BusinessObjects
{
	[NonPersistent]
	public abstract class RootObject : XPObject
	{
		DateTime _CreatedAt, _ModifiedAt;
		User _CreatedBy, _ModifiedBy;
		Guid _InstanceId = Guid.NewGuid();
		string _Tag;

		public RootObject(Session session)
				: base(session)
		{
		}
		public override void AfterConstruction()
		{
			base.AfterConstruction();
			if (Session.IsNewObject(this))
			{
				CreatedBy = Env.GetCurrentUser(base.Session);
				ModifiedBy = Env.GetCurrentUser(base.Session);
			}
		}
		protected override void OnSaving()
		{
			base.OnSaving();

			if (Session.IsObjectToSave(this))
			{
				ModifiedBy = Env.GetCurrentUser(base.Session);
				ModifiedAt = Env.GetServerDateTime(base.Session);
			}

			if (Session.IsNewObject(this))
			{
				CreatedBy = Env.GetCurrentUser(base.Session);
				CreatedAt = Env.GetServerDateTime(base.Session);
			}


		}
		[Browsable(false)]
		public User CreatedBy
		{
			get { return _CreatedBy; }
			set { SetPropertyValue(nameof(CreatedBy), ref _CreatedBy, value); }
		}
		[Browsable(false)]
		public User ModifiedBy
		{
			get { return _ModifiedBy; }
			set { SetPropertyValue(nameof(ModifiedBy), ref _ModifiedBy, value); }
		}
		[Browsable(false)]
		public DateTime CreatedAt
		{
			get { return _CreatedAt; }
			set { SetPropertyValue<DateTime>(nameof(CreatedAt), ref _CreatedAt, value); }
		}
		[Browsable(false)]
		public DateTime ModifiedAt
		{
			get { return _ModifiedAt; }
			set { SetPropertyValue<DateTime>(nameof(ModifiedAt), ref _ModifiedAt, value); }
		}

		[Browsable(false), Size(SizeAttribute.Unlimited)]
		public string Tag
		{
			get { return _Tag; }
			set { SetPropertyValue(nameof(Tag), ref _Tag, value); }
		}
		[NonPersistent]
		public Guid InstanceId
		{
			get { return _InstanceId; }
		}
	}
}
